﻿using System;
using System.Collections.Generic;
using System.Text;
using System.Xml;
using System.Xml.Linq;
using MongoDB.Bson.Serialization.Attributes;

namespace BlueLotus7.SSQAnalysisCore
{
    public class SSQLotteryStage : BaseBsonObject, ICloneable
    {
        protected const string xmlAttributeKey_OpenCode = "opencode";
        protected const string xmlAttributeKey_Identity = "expect";
        protected const string xmlAttributeKey_OpenDate = "opentime";

        #region Fields
        #endregion

        #region Properties

        public DateTime OpenDate
        {
            get;
            set;
        }

        public bool[] RedBalls
        {
            get;
            protected set;
        }

        public int BlueBall
        {
            get;
            protected set;
        }

        public string LotteryIdentity
        {
            get;
            protected set;
        }

        public bool this[int number]
        {
            get
            {
                number = number - 1;
                return (number >= 0 && number < RedBalls.Length && RedBalls[number] == true);
            }
        }

        #endregion

        #region Constructor

        /// <summary>
        /// Constructor
        /// </summary>
        /// <param name="lotteryIdentity"></param>
        /// <param name="value"></param>
        /// <param name="openDate"></param>
        public SSQLotteryStage(string lotteryIdentity, string value, DateTime openDate)
            : base()
        {
            this.LotteryIdentity = lotteryIdentity;
            this.OpenDate = openDate;
            this.RedBalls = new bool[Constant.RedBallQuantity];

            if (!string.IsNullOrEmpty(value))
            {
                try
                {
                    string[] temp = value.Split('|');
                    BlueBall = Convert.ToInt32(temp[1]);
                    string[] redBalls = temp[0].Split(',');
                    foreach (var one in redBalls)
                    {
                        this.RedBalls[Convert.ToInt32(one) - 1] = true;
                    }
                }
                catch
                {
                }
            }
        }

        /// <summary>
        /// Constructor
        /// </summary>
        /// <param name="lotteryIdentity"></param>
        /// <param name="openDate"></param>
        /// <param name="redBalls"></param>
        /// <param name="blueBall"></param>
        protected SSQLotteryStage(string lotteryIdentity, DateTime openDate, bool[] redBalls, int blueBall)
        {
            this.LotteryIdentity = lotteryIdentity;
            this.OpenDate = openDate;

            if (redBalls.Length == this.RedBalls.Length)
            {
                this.RedBalls = redBalls;
            }

            if (blueBall > 0 && blueBall <= Constant.BlueBallQuantity)
            {
                this.BlueBall = blueBall;
            }
        }

        #endregion

        /// <summary>
        /// Converts the value of this instance to a System.String.
        /// </summary>
        /// <returns>A string whose value is the same as this instance.</returns>
        public override string ToString()
        {
            StringBuilder builder = new StringBuilder();

            for (int i = 0; i < this.RedBalls.Length; i++)
            {
                if (this.RedBalls[i])
                {
                    builder.Append(" " + (i + 1).ToString("00"));
                }
            }

            builder.Append("|" + this.BlueBall.ToString("00"));

            return builder.ToString().Trim();
        }

        /// <summary>
        /// Converts SSQLottery from xml source to object instances.
        /// </summary>
        /// <param name="xmlDocument">Xml Document.</param>
        /// <returns>List of <c>SSQLotteryStage</c> object instances.</returns>
        public static List<SSQLotteryStage> ConvertSSQLottery(XDocument xDocument)
        {
            List<SSQLotteryStage> lotteries = new List<SSQLotteryStage>();

            if (xDocument != null && xDocument.Root.Elements() != null)
            {
                foreach (var row in xDocument.Root.Elements())
                {
                    //<row expect="12085" opencode="05,17,24,30,31,33|05" opentime="2012-07-22 21:30:00" ballsnum="4" />
                    SSQLotteryStage lottery = new SSQLotteryStage(
                        row.GetAttributeValue(xmlAttributeKey_Identity),
                        row.GetAttributeValue(xmlAttributeKey_OpenCode),
                        Convert.ToDateTime(row.GetAttributeValue(xmlAttributeKey_OpenDate)));
                    lotteries.Add(lottery);
                }
            }

            return lotteries;
        }

        /// <summary>
        /// Creates a new object that is a copy of the current instance.
        /// </summary>
        /// <returns> A new object that is a copy of this instance.</returns>
        public object Clone()
        {
            return new SSQLotteryStage(this.LotteryIdentity, this.OpenDate, this.RedBalls, this.BlueBall);
        }
    }
}
